template.defaults.imports.CountdownTime = function (factEndDate) {
    var timestamp = (new Date()).valueOf();
    return factEndDate - timestamp;
};


var loading = false; //状态标记
var hasNextPage = false;
var pageNo = 1;
var queryState = 1;//1 粉丝  2黑名单

queryList(pageNo);

//下拉刷新
$("#aroundBox").pullToRefresh().on("pull-to-refresh", function () {
    $("#weipai-list-content .left-list").html("");
    $("#weipai-list-content .right-list").html("");
    pageNo = 1;
    if (queryState == 1) {
        queryList(pageNo,function(){
            $("#aroundBox").pullToRefreshDone(); 
        });
    } else if (queryState == 2){
        queryBlackList(pageNo,function(){
            $("#aroundBox").pullToRefreshDone(); 
        }); 
    } 
   
});

//上拉加载更多
$('#aroundBox').infinite().on("infinite", function () {
    if (hasNextPage){
        pageNo++;
        if (queryState == 1) {
            queryList(pageNo,function(){ },true);
        } else if (queryState == 2){
            queryBlackList(pageNo,function(){ },true);
        } 

        
    }        
});

function queryList(pageNo,callback,isMore){
    if (loading) return;
    loading = true;
    var params = {};
    params.start = (pageNo - 1) * 20;
    params.limit = 20;
    params.shopId = shopId;
    /* global getCookie shopId*/
    params.token = getCookie("token");
    $.get('/antique/api/userShopCollection/query_user_list', params, function (result) {
        if (result.code == 0) {
            if (!isMore){
                /* global template */
                document.getElementById('contentTo').innerHTML = template('content', result.data);                
            } else {
                $("#contentTo").append(template('content', result.data));
            }
            hasNextPage = result.data.hasNextPage;
            if (!result.data.hasNextPage) {
                $('#loadmore').hide();
                $('#nomore').show();
            } else {
                $('#loadmore').show();
                $('#nomore').hide();
            }
            if (callback) callback();
            loading = false;
        }

        
    });
}

function queryBlackList(pageNo,callback,isMore){
    if (loading) return;
    loading = true;
    var params = {};
    params.start = (pageNo - 1) * 20;
    params.limit = 20;
    params.token = getCookie("token");
    $.get('/antique/api/userBlacklist/query_list', params, function (result) {
        if (result.code == 0) {
            if (!isMore){
                /* global template */
                document.getElementById('contentTo').innerHTML = template('blackContent', result.data);                
            } else {
                $("#contentTo").append(template('blackContent', result.data));
            }
            hasNextPage = result.data.hasNextPage;
            if (!result.data.hasNextPage) {
                $('#loadmore').hide();
                $('#nomore').show();
            } else {
                $('#loadmore').show();
                $('#nomore').hide();
            }
            if (callback) callback();
            loading = false;
        }

        
    });
}

$(".weui-tab .tabs .item").click(function () {
    $(".weui-tab .tabs .item").removeClass("current");
    $(this).addClass("current");
    queryState = $(this).data('state');
    $("#contentTo").html("");
    pageNo = 1;
    if (queryState == 1) {
        queryList(pageNo,function(){
            $("#aroundBox").pullToRefreshDone(); 
        });
    } else if (queryState == 2){
        queryBlackList(pageNo,function(){
            $("#aroundBox").pullToRefreshDone(); 
        }); 
    }
   
    
});
function addBlack (id){
    $.confirm("是否确认加入黑名单？", "", function() {
        $.post('/antique/api/userBlacklist/add_blacklist', { blackUserId:id,token:getCookie('token') }, function (result) {
            if (result.code == 0) {
                $.toast("操作成功！");
                $("#contentTo").html("");
                pageNo = 1;
                queryList(pageNo,function(){},true);
            } else {
             $.toast(result.message, "cancel");
            }
         });
      }, function() {
        //取消操作
      });
}
function canalBlack (id){
    $.confirm("是否确认移出黑名单？", "", function() {
        $.post('/antique/api/userBlacklist/delete_blacklist', { id:id,token:getCookie('token') }, function (result) {
            if (result.code == 0) {
                $.toast("操作成功！");
                $("#contentTo").html("");
                pageNo = 1;
                queryBlackList(pageNo,function(){},true);
            } else {
             $.toast(result.message, "cancel");
            }
         });
      }, function() {
        //取消操作
      });
}
function charToUser(userId){
    if(checkUserMe(userId)){
        $.toast('自己和自己不能聊天', "cancel");
    } else {
        visitUrl('/chat?charToUser=' + userId ,true);
    }
}

